from mcp import ClientSession
from mcp.client.sse import sse_client #创建sse客户端
from rich.console import Console
from rich.panel import Panel

console = Console()

async def run():
    # 根据上下文协议建立联系
    async with sse_client(url="http://localhost:8000/sse") as streams:
        async with ClientSession(*streams) as session:
            await session.initialize()

            tools = await session.list_tools()
            console.print(tools)
            # 调用工具
            result = await session.call_tool("add", arguments={
                "a": 2,
                "b": 3
            })
            resources = await session.list_resources()
            console.print(resources)

            say_hello_content = await session.read_resource("say://tony")
            console.print(Panel(f"{say_hello_content.contents[0].text}", title="say hello resource"))

            console.print(Panel(f"2 + 3 = {result.content[0].text}", title="call add tool"))

            prompt_result = await session.get_prompt("get_history_messages", arguments={
                "query": "some query"
            })

            console.print(prompt_result)

if __name__ == "__main__":
    import asyncio
    asyncio.run(run())